import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/a_mine/models/mine_entity.dart';
import 'package:ls_work/a_mine/models/week_record_entity.dart';
import 'package:ls_work/common/common.dart';
import 'package:ls_work/mvp/svp_progress.dart';
import 'package:ls_work/net/HttpUtil.dart';
import 'package:ls_work/net/net.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/res/resources.dart';
import 'package:ls_work/a_mine/widget/me_top_view.dart';
import 'package:ls_work/a_mine/widget/me_week_list_cell.dart';
import 'package:ls_work/a_mine/my_router.dart';
import 'package:ls_work/routers/fluro_navigator.dart';
import 'package:ls_work/a_mine/widget/me_normal_cell.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart' as FlutterStars;
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:ls_work/util/event_bus.dart' as e;
import 'package:ls_work/util/toast.dart';

import '../../ui/elder_filter_setting_page.dart';
import '../../ui/patrol/patrol_nfc_write_page.dart';

class MyPage extends StatefulWidget {
  @override
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  int humanId = 0;
  List<WeekInfo> dataList = [];

  MineBaseInfo baseInfo = MineBaseInfo();

  @override
  void initState() {
   e.eventBus.on(Constant.eventUserLogin, (arg) {
      if (mounted) {
        // setState(() {
        //   _fectData();
        // });
      }
    });

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      humanId = FlutterStars.SpUtil.getInt(Constant.humanId)??0;
      _fectData();
      _fectstaffBasicInfoData();
    });
    super.initState();
  }

  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('个人中心'),
        elevation: 0.5,
      ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            MeTopView(
              info: baseInfo,
            ),
            Container(
              height: 5,
              color: Colours.bg_color,
            ),
            dataList.length > 0
                ? MeWeekListCell(infoList: dataList)
                : Container(),
            Container(
              height: 5,
              color: Colours.bg_color,
            ),
            MeNormalCell(
                title: '健康证',
                icon: 'mine/me_tag1',
                onTap: () {
                  NavigatorUtils.push(context, MyRouter.myHealth);
                }),
            MeNormalCell(
                title: '资格证书',
                icon: 'mine/me_tag2',
                onTap: () {
                  NavigatorUtils.push(context, MyRouter.myQualifications);
                }),
            MeNormalCell(
                title: '意见反馈',
                icon: 'mine/me_tag3',
                onTap: () {
                  NavigatorUtils.push(context, MyRouter.feedBack);
                }),
            MeNormalCell(
                title: '巡防点Nfc标签写入',
                icon: 'mine/me_tag3',
                onTap: () {
                  Go().push(PatrolWriteHomePage());
                }),
            MeNormalCell(
                title: '设置',
                icon: 'mine/me_tag4',
                onTap: () {
                  NavigatorUtils.push(context, MyRouter.mySetting);
                }),

            MeNormalCell(
                title: '长者默认筛选设置',
                icon: 'mine/me_tag5',
                onTap: () {
                  Go().push(ElderFilterSettingPage());
                  // NavigatorUtils.push(context, MyRouter.mySetting);
                }),

          ],
        ),
      ),
    );
  }

  _fectData() {
    var map = {'humanId': humanId};
    SVProgressHUD page = SVProgressHUD(context);
    page.showProgress();
    HttpUtil.instance
        .getDio()
        .post(HttpApi.detailRecordOneWeek, data: map,options: PostRequest.urlencodedHeader)
        .then((r) {
      ///
      page.closeProgress();
      if (r.statusCode == 200) {
        Map<String, dynamic> map = json.decode(r.toString());
        if (map["code"].toString() == "0") {
          if (map['data'] != null) {
            WeekRecordEntity model = WeekRecordEntity.fromJson(map['data']);
            setState(() {
              dataList = model.records??[];
            });
          } else {
            Toast.show(map["msg"]);
          }
        } else {
          Toast.show(map["msg"]);
        }
      } else {
        Toast.show(r.statusCode.toString());
      }
    }).catchError((e) {
      page.closeProgress();
      print(e.toString());
      Toast.show(getErrorMessage(e));
    });
  }

  _fectstaffBasicInfoData() {
    SVProgressHUD page = SVProgressHUD(context);
    page.showProgress();
    HttpUtil.instance.getDio().post(HttpApi.staffBasicInfo).then((r) {
      ///
      page.closeProgress();
      if (r.statusCode == 200) {
        Map<String, dynamic> map = json.decode(r.toString());
        if (map["code"].toString() == "0") {
          if (map['data'] != null) {
            MineBaseInfo model = MineBaseInfo.fromJson(map['data']);
            setState(() {
              baseInfo = model;
            });
          } else {
            Toast.show(map["msg"]);
          }
        } else {
          Toast.show(map["msg"]);
        }
      } else {
        Toast.show(r.statusCode.toString());
      }
    }).catchError((e) {
      page.closeProgress();
      print(e.toString());
      Toast.show(getErrorMessage(e));
    });
  }
}
